#用于游客和管理员登录
#用于游客的注册
from tkinter.constants import S
import psycopg2

class loginSearch():
    def __init__(self):
        self.get_conn()

    def get_conn(self):
        try:
            self.conn = psycopg2.connect(
                database='taobao',
                user='postgres',
                password='dch20001121',
                host='127.0.0.1',
                port='5432'
            )
        except psycopg2.Error as e:
            print("Link Error %s " % e)
    
    def close_conn(self):
        try:
            if self.conn:
                self.conn.close()
        except psycopg2.Error as e:
            print("Close Error %s " % e)

    def get_info(self):
        #用于获取管理员信息
        sql = "select * from administrators"
        cursor = self.conn.cursor()
        cursor.execute(sql)
        accounts = cursor.fetchall()
        #将账号和密码以字典形式存储 k-v
        result = {}
        for account in accounts:
            # print(account[0], account[1])
            result[account[0]] = account[1]

        return result

    def get_info_v(self):
        #用于获取访客信息
        sql = "select * from user_imformation"
        cursor = self.conn.cursor()
        cursor.execute(sql)
        accounts = cursor.fetchall()
        result = {}

        for account in accounts:
            result[account[0]] = account[1]

        return result

    def insert_info_v(self, name, value):
        sql = '''insert into user_imformation(user_id, pwd)
                 values
                 ({name}, '{value}');   
            '''.format(name=name, value=value)
        cursor = self.conn.cursor()
        cursor.execute(sql)
        self.conn.commit()